#ifndef __DALELIB_SKYBOX_H__
#define __DALELIB_SKYBOX_H__

#include <dalelib/maths.h>
#include <dalelib/graphics.h>
#include <dalelib/graphics/texture.h>

using namespace dalelib;

namespace dalelib
{

static v3 skybox_verts[8] =
{
	v3(-1,-1,-1), // 0
	v3( 1,-1,-1), // 1
	v3( 1, 1,-1), // 2
	v3(-1, 1,-1), // 3
	v3(-1,-1, 1), // 4
	v3( 1,-1, 1), // 5
	v3( 1, 1, 1), // 6
	v3(-1, 1, 1), // 7
};

class Skybox
{
private:

	Texture _faces[6];

public:


	void load(const string _FileName,const string _Suffix)
	{
		_faces[0].load(_FileName+"_negative_x."+_Suffix);
		_faces[1].load(_FileName+"_positive_x."+_Suffix);
		_faces[2].load(_FileName+"_negative_y."+_Suffix);
		_faces[3].load(_FileName+"_positive_y."+_Suffix);
		_faces[4].load(_FileName+"_negative_z."+_Suffix);
		_faces[5].load(_FileName+"_positive_z."+_Suffix);
	}

	void drawFace(int face,int v1,int v2,int v3,int v4)
	{
		_faces[face].bind();
		glBegin(GL_QUADS);
			glTexCoord2f(1,1); glVertex3fv(skybox_verts[v1].ptr());
			glTexCoord2f(0,1); glVertex3fv(skybox_verts[v2].ptr());
			glTexCoord2f(0,0); glVertex3fv(skybox_verts[v3].ptr());
			glTexCoord2f(1,0); glVertex3fv(skybox_verts[v4].ptr());
		glEnd();
		_faces[face].unbind();
	}

	void draw(const float _Scale=1.f)
	{
		glDisable(GL_BLEND);
		glDisable(GL_LIGHTING);
		glDisable(GL_LIGHTING);
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_DEPTH_TEST);

		glPushMatrix();
		glScalef(_Scale,_Scale,_Scale);

		drawFace(0, 4,0,3,7); // -x
		drawFace(1, 1,5,6,2); // +x
		drawFace(2, 4,5,1,0); // -y
		drawFace(3, 6,7,3,2); // +y
		drawFace(4, 0,1,2,3); // -z
		drawFace(5, 5,4,7,6); // +z

		glPopMatrix();
	}

};

} // namespace dalelib

#endif
